package com.ruitu.mall_98_1.mvp.presenter;

import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98_1.mvp.contract.ProductDetailContract;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.product.ProDetail;
import com.ruitu.router_module.bean.product.SpecInfo;

/**
 * Created by Administrator on 2018/11/06
 */
public class ProductDetailPresenterImpl extends ProductDetailContract.Presenter {

    @Override
    public void reqProDetail(String goodId, String accountId) {
        mModel.reqProDetail(goodId, accountId).subscribe(new SubscriberImpl<BaseResult<ProDetail>>() {
            @Override
            public void onError(Throwable e) {
                mView.hideProgress();
                e.printStackTrace();
//                ToastUtils.showShort("获取商品详情失败,请重试");
                mView.onReqComplete(false, 1, null);
            }

            @Override
            public void onNext(BaseResult<ProDetail> proDetailResult) {
                mView.hideProgress();
                if (proDetailResult.isSuccess()) {
                    mView.onReqComplete(true, 1, proDetailResult.getData());
                } else {
                    ToastUtils.showShort(proDetailResult.getMsg());
                }
            }
        });
    }

    @Override
    public void reqProSpec(String goodId) {
        mModel.reqProSpec(goodId).subscribe(new SubscriberImpl<BaseResult<SpecInfo>>() {
            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(BaseResult<SpecInfo> listResult) {
                if (listResult.isSuccess()) {
                    mView.onReqComplete(true, 2, listResult.getData());
                } else {
                    ToastUtils.showShort(listResult.getMsg());
                }
            }
        });
    }

    @Override
    public void addToCart(String goodsId, String accountId, int num, String skuId) {
        mModel.addToCart(goodsId, accountId, num, skuId).subscribe(new SubscriberImpl<BaseResult>() {
            @Override
            public void onError(Throwable e) {
                mView.hideProgress();
            }

            @Override
            public void onNext(BaseResult baseResult) {
                mView.hideProgress();
                if (baseResult.isSuccess()) {
                    ToastUtils.showShort("已添加到购物车");
                } else {
                    ToastUtils.showShort(baseResult.getMsg());
                }
            }
        });
    }

    @Override
    public void reqGetCoupon(String accountId, String couponId) {
        mModel.reqGetCoupon(accountId, couponId).subscribe(new SubscriberImpl<BaseResult>() {
            @Override
            public void onError(Throwable e) {
                mView.hideProgress();
            }

            @Override
            public void onNext(BaseResult baseResult) {
                mView.hideProgress();
                if (baseResult.isSuccess()) {
                    ToastUtils.showShort("领券成功!");
                } else {
                    ToastUtils.showShort(baseResult.getMsg());
                }
            }
        });
    }

    @Override
    public void reqcollect(String accountId, String type, String infoId) {
        mModel.reqcollect(accountId, type, infoId).subscribe(new SubscriberImpl<BaseResult>() {
            @Override
            public void onError(Throwable e) {
                mView.hideProgress();
            }

            @Override
            public void onNext(BaseResult baseResult) {
                mView.hideProgress();
                if (baseResult.isSuccess()) {
                    ToastUtils.showShort("收藏成功");
                    mView.onReqComplete(true, 3, null);
                } else {
                    ToastUtils.showShort(baseResult.getMsg());
                }
            }
        });
    }

    @Override
    public void reqcollectCancel(String accountId, String type, String id) {
        mModel.reqcollectCancel(accountId, type, id).subscribe(new SubscriberImpl<BaseResult>() {
            @Override
            public void onError(Throwable e) {
                mView.hideProgress();
            }

            @Override
            public void onNext(BaseResult baseResult) {
                mView.hideProgress();
                if (baseResult.isSuccess()) {
                    ToastUtils.showShort("取消收藏成功");
                    mView.onReqComplete(true, 4, null);
                } else {
                    ToastUtils.showShort(baseResult.getMsg());
                }
            }
        });
    }
}